<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>解决 android.os.FileUriExposedException: file:///storage/emulated/0/photo.jpeg exposed beyond app t...</title>
    <style type="text/css" media="all">
      body {
        margin: 0;
        font-family: "Helvetica Neue", Helvetica, Arial, "Hiragino Sans GB", sans-serif;
        font-size: 14px;
        line-height: 20px;
        color: #777;
        background-color: white;
      }
      .container {
        width: 700px;
        margin-right: auto;
        margin-left: auto;
      }

      .post {
        font-family: Georgia, "Times New Roman", Times, "SimSun", serif;
        position: relative;
        padding: 70px;
        bottom: 0;
        overflow-y: auto;
        font-size: 16px;
        font-weight: normal;
        line-height: 25px;
        color: #515151;
      }

      .post h1{
        font-size: 50px;
        font-weight: 500;
        line-height: 60px;
        margin-bottom: 40px;
        color: inherit;
      }

      .post p {
        margin: 0 0 35px 0;
      }

      .post img {
        border: 1px solid #D9D9D9;
      }

      .post a {
        color: #28A1C5;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="post">
        <h1 class="title">解决 android.os.FileUriExposedException: file:///storage/emulated/0/photo.jpeg exposed beyond app t...</h1>
        <div class="show-content">
          <p><b>看客朋友们大家好, 我是大表哥, 欢迎收看本期内容:<br></b></p><p>本期内容说的是关于解决 android 9.0 拍照报错的问题, 废话不多说, 看正题:</p><p>前言: 修改别人项目时发现点击相机拍照奔溃了, 于是就有了这篇文章;</p><p>先看错误信息: </p><p><b>android.os.FileUriExposedException: file:///storage/emulated/0/photo.jpeg exposed beyond app through ClipData.Item.getUri()</b></p><p><b>这个问题是 android 7.0 及以后版本在系统api上进行了修改, 导致以前调用相机拍照的功能奔溃, </b></p><p><b>解决方案1: </b></p><p><b>在Application的onCreat()方法中添加以下代码：<br></b></p><p>        // android 7.0 拍照问题</p><p>        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();</p><p>        StrictMode.setVmPolicy(builder.build());</p><p>        builder.detectFileUriExposure();</p><p><br></p><p>解决方案2</p><p>在AndroidManifest.xml中添加如下代码</p><p>&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"    ...</p><p>    &lt;application</p><p>        ...</p><p>        &lt;provider</p><p>            android:name="android.support.v4.content.FileProvider"            android:authorities="${applicationId}.provider"            android:exported="false"            android:grantUriPermissions="true"&gt;&lt;meta-data</p><p>                android:name="android.support.FILE_PROVIDER_PATHS"                android:resource="@xml/provider_paths"/&gt;&lt;/provider&gt;&lt;/application&gt;&lt;/manifest&gt;</p><p><br></p><p>在res目录下新建一个xml文件夹，并且新建一个provider_paths的xml文件</p><p>&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;paths xmlns:android="http://schemas.android.com/apk/res/android"&gt;&lt;external-path name="external_files" path="."/&gt;&lt;/paths&gt;</p><p><br></p><p>代码</p><p>将: Uri photoURI = Uri.fromFile(createImageFile());</p><p>改成: Uri photoURI = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", createImageFile());</p>
        </div>
      </div>
    </div>
  </body>
</html>
